package com.sctu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.FamousOneInfoActivity;
import com.sctu.EMSApp.R;
import com.sctu.EMSApp.ScenictourRoundClickAvtivity;
import com.sctu.adapter.FamousAdapter;
import com.sctu.adapter.RoundAdapter;
import com.sctu.bean.Famous;
import com.sctu.bean.FamousItem;
import com.sctu.bean.RoundBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.List;

/**
 * Created by zhu on 2015/11/3.
 */
public class ScenictourRoundFragment extends Fragment {

    private ListView listView;
    private ImageView imageView;
    private View view;
    private RatingBar mRatingBar;
    private List<RoundBean.Results> resultss;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_scenictour_round, container, false);

        listView = (ListView) view.findViewById(R.id.fragment_scenicTour_round_listView);
        imageView = (ImageView) view.findViewById(R.id.round_bac_box);


//        initheadimg();
        initlistview();
//        init();
        return view;

    }

    private void initlistview() {

        // 发起网络请求

        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_SCENIC_ROUND,
                listener, errorlistener);


    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {

            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            final RoundBean results = JSON.parseObject(jsonstring, RoundBean.class);
            resultss = results.getResults();

            //建立适配器，并绑定数据源绑定适配器到listview
            listView.setAdapter(new RoundAdapter(resultss,getActivity()));
            //监听每一个listitem并控制相应的跳转
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.i("mm", " onItemClick ");
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), ScenictourRoundClickAvtivity.class);
                    intent.putExtra("id", resultss.get(position).getA_id());
                    getActivity().startActivity(intent);
                    view.setBackgroundResource(R.color.list_grey);
                }
            });

        }

    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();

        }
    };

//
//    private void initheadimg() {
//
//        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_FAMOUS_HEAD,
//                listenerhead, errorlistenerhead);
//    }
//
//    // 当网络请求返回成功（200的状态）
//    Response.Listener<String> listenerhead = new Response.Listener<String>() {
//
//        @Override
//        public void onResponse(String jsonstring) {
//            Log.e("Json", jsonstring);
//
//            // 开始解析JSON
//            // 反序列化，直接将JSON反序列化为Test这个Class对象
//            Famous results = JSON.parseObject(jsonstring, Famous.class);
//            List<Famous.Results> rr = results.getResults();
//            //设置图片
//            GetVolleyHttp.getInstance(getActivity()).setImageLoader(imageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + rr.get(0).getPath());
//        }
//    };
//
//    // 当网络请求返回状态不成功（也就不是200）
//    Response.ErrorListener errorlistenerhead = new Response.ErrorListener() {
//
//        @Override
//        public void onErrorResponse(VolleyError arg0) {
//            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
//                    .show();
//
//        }
//    };

//
//    private void init() {
//        mRatingBar = (RatingBar) view.findViewById(R.id.round_ratingbar);
//        //为RatingBar设置监听
//        mRatingBar.setOnRatingBarChangeListener(new RatingBarChangeListenerImpl());
//        //得到RatingBar的最大等级
//        int max = mRatingBar.getMax();
//        //得到RatingBar现在的等级
//        float currentRating = mRatingBar.getRating();
//        System.out.println("max=" + max + ",currentRating" + currentRating);
//    }
//
//    private class RatingBarChangeListenerImpl implements RatingBar.OnRatingBarChangeListener {
//        @Override
//        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
//
//            System.out.println("现在的等级为 rating="+rating+",是否是用户触发 fromUser="+fromUser);
//        }
//
//    }
}
